package cn.modoumama.notice.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.modoumama.notice.entity.ApiMethodCfgModel;
import cn.modoumama.notice.entity.ApiMethodModel;
import cn.modoumama.notice.mapper.ApiMethodMapper;
import cn.modoumama.notice.service.ApiMethodCfgService;
import cn.modoumama.notice.service.ApiMethodService;
import cn.modoumama.service.base.impl.BaseServiceImpl;


/**
* 类名称：ApiMethod对象定义<br>
* 类描述：方法表业务实现类<br>
* 类生产方式：工具自动生成代码<br>
* 创建人：dengqiang<br>
* 创建时间：2017-06-16 13:45:27<br>
* 修改人：  <br>
* 修改时间：<br>
* 修改备注：<br>
* @version   V1.0
*/
@Service
@Transactional(value="notice", rollbackFor={Exception.class})
public class ApiMethodServiceImpl extends BaseServiceImpl<ApiMethodModel,Integer> implements ApiMethodService{
	Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	ApiMethodCfgService apiMethodCfgService;
	
	@Autowired
	public void setMapper(ApiMethodMapper genericMapper) {
		super.setGenericMapper(genericMapper);
	}

	@Override
	public Integer mergeMethod(ApiMethodModel apiMethodModel){
		
		if(apiMethodModel.getApiMethodCfgs().size()>0){
			ApiMethodModel old = new ApiMethodModel();
			old.setMethod(apiMethodModel.getMethod());
			old = findObjectByModel(old);
			if(old == null){
				insert(apiMethodModel);
			}else{
				apiMethodModel.setId(old.getId());
				apiMethodModel.setInvokeDayMaxNum(null);
				apiMethodModel.setInvokeMinMaxNum(null);
				updateById(apiMethodModel);
			}
			for (ApiMethodCfgModel apiMethodCfgModel : apiMethodModel.getApiMethodCfgs()) {
				apiMethodCfgModel.setMethodId(apiMethodModel.getId());
				apiMethodCfgService.mergeMethodCfg(apiMethodCfgModel);
			}
		}
		
		return apiMethodModel.getId();
	}
}
